<?php  
if (!defined('ZAZA')) exit('No direct script access allowed');
/**
 * @copyright     ZaZa Group
 * @author        Sir Doan
 */

require_once ROOT_PATH . DS .'kernel'. DS .'clean'. EXT;
class Input{
    protected static $arrValue = array (); //@param mang luu gia tri cac bien GET tu url  
    protected static $arrValueObjectDefault = array(); //@param mang luu cac gia tri mac dinh khoi tao cua 1 object
    protected static $arrObjectIsCreate = array(); // @param mang lua cac object da duoc khoi tao roi
    function __construct() { }
    
    /**
     * Object Get
     * 
     * @param String $objName
     * @param String $objType [default: 'str']
     * @param $defaultValue [default: null]   
     * @return String or Int
     * @access Public
     */
    private static function _get($objName, $objType = 'str', $defaultValue = '') 
    {
        $value = ''; 
        if(isset(self::$arrValue[$objName])) 
            return self::$arrValue[$objName];
        elseif(isset($_GET[$objName]))
            $value = $_GET[$objName];
        // Default Value       
        if($value == '')
            $value = $defaultValue; 
        $value = self::objConvert($value, $objType);  
        self::$arrValue[$objName] = $value;
        return $value;
    }   
    
    /**
     * Object Post
     * 
     * @param String $objName
     * @param String $objType [default: 'str']
     * @param $defaultValue [default: null]   
     * @return String or Int
     * @access Public
     */
    private static function _post($objName, $objType = 'str', $defaultValue = '') 
    {
        $value = ''; 
        if(isset($_POST[$objName]))
            $value = $_POST[$objName];
        // Default Value       
        if($value == '')
            $value = $defaultValue; 
        $value = self::objConvert($value, $objType); 
        return $value;   
    }
    
    /**
     * Object Get & Post
     * 
     * @param String $objName
     * @param String $objType [default: 'str']
     * @param $defaultValue [default: null]   
     * @return String or Int
     * @access Public
     */
    public static function get($objName, $objType = 'str', $defaultValue = '') 
    {
        // Value defaule
        $default = self::objConvert($defaultValue, $objType);
        // Post & Get
        if(isset($_POST[$objName]))  
            $value = self::_post($objName, $objType, $defaultValue); 
        elseif(isset(self::$arrValue[$objName]))
            return self::$arrValue[$objName];
        elseif(isset($_GET[$objName]))             
            $value = self::_get($objName, $objType, $defaultValue);
        else 
            return $default;
        return $value;    
    }
    
    /**
     * Convert Object
     *   
     * @param String $value
     * @param String $type
     * @return String or Int
     * @access Private
     */
    private static function objConvert($value, $type = 'str') {
        $objClean = new Clean(); //Input::createObject('Clean');
        switch($type) 
        {
            case 'def' :   
                if(!is_array($value)) 
                    $value = stripcslashes($value);
                $newValue = $objClean->xss_clean($value);
                break;
            case 'int' :
                $newValue = (int)$value;
                break;
            case 'double' :
                $value = str_replace ( '.', '', $value );
                $value = str_replace ( ',', '.', $value );
                $newValue = doubleval ( $value );
                break;
            /*case 'currency_format' :
                $newValue = self::currencyFormat ( $value );
                break; */
            case 'str' :
                if($value) $newValue = $objClean->xss_clean($value); 
                else $newValue = '';
                break;
            default :
                $newValue = $objClean->xss_clean(stripcslashes($value));
                break;  
        }
        return $newValue;
    }
     
}
?>